package com.ccs.loas;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class Hcs_Methods {
	
	
	public void go_to_gu_landing_page(WebDriver driver){
		driver.findElement(By.xpath("//a[contains(.,'General Units Landing Page')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void search_pin(WebDriver driver, String pin){
		driver.findElement(By.xpath("//input[@id='criteria']")).sendKeys(pin);
		driver.findElement(By.xpath("//input[@name='search']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void add_pf_doctor(WebDriver driver){
		Select action = new Select(driver.findElement(By.xpath("//table[@id='occupancyList']/tbody/tr/td[6]/select")));
		action.selectByVisibleText("Doctor and PF Amount");
		driver.findElement(By.xpath("//table[@id='occupancyList']/tbody/tr/td[7]/input")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//tbody[@id='admissionDoctorBeanRows']/tr/td[6]/input[2]")).click();
		driver.findElement(By.xpath("//input[@id='btnAddPf']")).click();
		
		WebElement pfAmount = driver.findElement(By.xpath("//input[@id='pfAmountInput']"));
		pfAmount.clear();
		pfAmount.sendKeys("1000");
		Select pfType = new Select(driver.findElement(By.xpath("//select[@id='pfTypeCode']")));
		pfType.selectByVisibleText("COLLECT");
		driver.findElement(By.xpath("//input[@id='btnAddPf']")).click();
		driver.findElement(By.xpath("//input[@value='Save']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void add_second_doctor(WebDriver driver){
		String mainWindow = driver.getWindowHandle();
		driver.findElement(By.xpath("//input[@id='btnAddDoctor']")).click();
		driver.findElement(By.xpath("//input[@id='searchDoctorPopup']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@id='entity_finder_key']")).sendKeys("0900");
		driver.switchTo().window(mainWindow);
		Actions actions = new Actions(driver);
        WebElement mouseover1 = driver.findElement(By.xpath("//div[@id='doctorFinderForm']/div[2]/div[1]/input[2]"));
        actions.moveToElement(mouseover1);
        actions.click().perform();
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		driver.findElement(By.xpath("//input[@id='btnAddDoctor']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//tbody[@id='admissionDoctorBeanRows']/tr[2]/td[6]/input[2]")).click();
		driver.findElement(By.xpath("//input[@id='btnAddPf']")).click();
		
		WebElement pfAmount = driver.findElement(By.xpath("//input[@id='pfAmountInput']"));
		pfAmount.clear();
		pfAmount.sendKeys("500");
		Select pfType = new Select(driver.findElement(By.xpath("//select[@id='pfTypeCode']")));
		pfType.selectByVisibleText("COLLECT");
		driver.findElement(By.xpath("//input[@id='btnAddPf']")).click();
		driver.findElement(By.xpath("//input[@value='Save']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void clinic_discharge(WebDriver driver){
		driver.findElement(By.xpath("//input[@id='dischargeAction']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//div[@id='dischargeForm']/input[@value='Discharge']")).click();
		System.out.println("Discharged on Nursing Unit");
	}
	public void go_to_general_units(WebDriver driver){
		driver.findElement(By.xpath("//a[contains(.,'General Units')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void go_to_billing_landing_page(WebDriver driver){
		driver.findElement(By.xpath("//a[contains(.,'Patient Billing and Accounting Landing Page')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void billing_guarantor_checking(WebDriver driver, String loaNumber, String option){
		
		driver.findElement(By.xpath("//table[@id='row']/tbody/tr/td[15]/input")).click();
		driver.findElement(By.xpath("//a[@id='updateLink']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		List<WebElement> rows = driver.findElements(By.xpath("//div[@id='hmoDoctors']/table/tbody/tr"));
		int size = rows.size();
		
		for(int x=1; x<=size; x++){
			String text = driver.findElement(By.xpath("//div[@id='hmoDoctors']/table/tbody/tr["+x+"]/td[3]")).getText();
			text = text.replaceAll(" ", "");
			if(text.equals("500.00")){
				driver.findElement(By.xpath("//div[@id='hmoDoctors']/table/tbody/tr["+x+"]/td[4]/input")).click();
			}
		}
		
		//String doctor1 = driver.findElement(By.xpath("//input[@id='chkCovered0']")).getAttribute("onclick");
		//String doctor2 = driver.findElement(By.xpath("//input[@id='chkCovered1']")).getAttribute("onclick");
		//System.out.println(doctor1);
		//System.out.println(doctor2);
		
		//if(doctor1.contains("0")){
		//driver.findElement(By.xpath("//input[@id='chkCovered0']")).click();
		//}
		//if(doctor2.contains("0")){
		//	driver.findElement(By.xpath("//input[@id='chkCovered1']")).click();
		//}
		
		//Compare loaNumber to hcs
		String loaNo = driver.findElement(By.xpath("//input[@id='loa.loaNo']")).getAttribute("value");
		loaNumber = loaNumber.toUpperCase().toString();
		if(loaNo.equals(loaNumber)){
			System.out.println("Correct Loa Number");
		}else{
			System.out.println("Incorrect Loa Number");
		}
		
		String amount = driver.findElement(By.xpath("//input[@id='loa.maximumAmount']")).getAttribute("value");
		String pf = driver.findElement(By.xpath("//input[@id='loa.maximumPfAmount']")).getAttribute("value");
		
		if(option.equals("covered")){
			
			//Check Maximum Amount Detail
			if(amount.equals("10500.0")){
				System.out.println("Correct Maximum Amount of Loa");
			}else{
				System.out.println("Incorrect Maximum Amount of Loa");
			}
		
			//Check PF Detail
			if(pf.equals("2000.0")){
				System.out.println("Correct PF of Loa");
			}else{
				System.out.println("Incorrect PF of Loa");
			}
			
		}else{
			
			//Check Maximum Amount Detail
			if(amount.equals("1000.0")){
				System.out.println("Correct Maximum Amount of Loa");
			}else{
				System.out.println("Incorrect Maximum Amount of Loa");
			}
		
			//Check PF Detail
			if(pf.equals("1500.0")){
				System.out.println("Correct PF of Loa");
			}else{
				System.out.println("Incorrect PF of Loa");
			}	
		}
			
		
		driver.findElement(By.xpath("//input[@value='Submit']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void generate_billing_soa(WebDriver driver){
		
		//SKIP GUARANTOR
		driver.findElement(By.xpath("//input[@name='formAction']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP ROOM
		driver.findElement(By.xpath("//input[@name='skipButton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP PHILHEALTH
		driver.findElement(By.xpath("//input[@id='btnSkip']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP DISCOUNT
		driver.findElement(By.xpath("//input[@id='skipBtn']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP GENERATION OF UNOFFICIAL SOA
		driver.findElement(By.xpath("//input[@name='submitButton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@value='Print SOA']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@value='Generate Official SOA']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		//GENERATE OFFICIAL SOA
		driver.findElement(By.xpath("//input[@id='showHeader']")).click();
		driver.findElement(By.xpath("//input[@value='Submit']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@value='Go To Landing Page']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		System.out.println("Patient Bill Discharged.");
		
	}
	public void cash_payment(WebDriver driver){
		
		//SKIP GUARANTOR
		driver.findElement(By.xpath("//input[@name='formAction']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP ROOM
		driver.findElement(By.xpath("//input[@name='skipButton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP PHILHEALTH
		driver.findElement(By.xpath("//input[@id='btnSkip']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP DISCOUNT
		driver.findElement(By.xpath("//input[@id='skipBtn']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP GENERATION OF UNOFFICIAL SOA
		driver.findElement(By.xpath("//input[@name='submitButton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@id='cashPaymentMode1']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		String payment = driver.findElement(By.xpath("//span[@id='totalAmountDue']")).getText();
		System.out.println("Amount Due: "+ payment);
		if(payment.contains(",")){
			payment = payment.replaceAll(",","");
		}
		driver.findElement(By.xpath("//input[@id='cashBillAmount']")).sendKeys(payment);
		driver.findElement(By.xpath("//input[@value='Proceed with Payment']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@value='Print SOA']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@value='Generate Official SOA']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		//GENERATE OFFICIAL SOA
		driver.findElement(By.xpath("//input[@id='showHeader']")).click();
		driver.findElement(By.xpath("//input[@value='Submit']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@value='Go To Landing Page']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		System.out.println("Patient Bill Discharged.");
	}
	public void goto_philhealth_form(WebDriver driver, String pin){
		
		driver.manage().window().maximize();
		try{
			driver.findElement(By.xpath("//a[@href='/pba/pbaHome.html']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}catch(Exception e){}
		driver.findElement(By.xpath("//input[@id='criteria']")).sendKeys(pin);
		driver.findElement(By.xpath("//input[@name='search']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		Select action = new Select(driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td[8]/select")));
		action.selectByVisibleText("PhilHealth");
		driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td[9]/input")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void philhealth_form(WebDriver driver){
		
		driver.findElement(By.xpath("//input[@id='btnDiagnosisLookup']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@id='icd10_entity_finder_key']")).sendKeys("A00");
		driver.findElement(By.xpath("//div[@id='icd10FinderForm']/div[2]/div[1]/input[1]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//div[@class='searchResultsContainer']/div[3]/table/tbody/tr[1]/td[1]/a")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		Select action = new Select(driver.findElement(By.xpath("//select[@id='phCaseRateType']")));
		action.selectByVisibleText("SURGICAL");
		
		driver.findElement(By.xpath("//input[@id='btnCaseRateLookup']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@id='rvs_entity_finder_key']")).sendKeys("15934");
		driver.findElement(By.xpath("//div[@id='rvsAction']/input[1]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//div[@id='rvsTable']/div[2]/table/tbody/tr/td[1]/a")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@id='memberInfo.membershipID']")).sendKeys("33333");
		
		Select actions = new Select(driver.findElement(By.xpath("//select[@id='phCaseRateTypeSecond']")));
		actions.selectByVisibleText("SURGICAL");
		
		driver.findElement(By.xpath("//input[@id='btnCaseRateLookupSecond']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@id='rvu_entity_finder_key']")).sendKeys("19220");
		driver.findElement(By.xpath("//div[@id='rvuFinderForm']/div[2]/div[1]/input[2]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//div[@id='rvuFinderForm']/div[2]/div[2]/table/tbody/tr/td[1]/div")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@id='1stCaseDoctor-0']")).click();
		driver.findElement(By.xpath("//input[@id='2ndCaseDoctor-1']")).click();
		
		driver.findElement(By.xpath("//input[@id='btnCompute']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//input[@id='btnSave']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//a[contains(.,'Home')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	
	
}
